package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.GameScene;
import com.hitechbangla.fctu.base.Location;
import com.hitechbangla.fctu.base.base.BaseType;
import java.awt.MouseInfo;
import java.awt.Point;
import java.util.List;
import resource_builder.GenericTypeBuilder;
import com.hitechbangla.fctu.base.base.Base;
import com.hitechbangla.fctu.utility.GeneralUtil;
import com.hitechbangla.fctu.utility.RecordedGame;
import fctu.simulator.HTB_MAIN;
import java.awt.geom.Point2D;
import org.openstreetmap.gui.jmapviewer.OsmMercator;

/**
 *
 * @author deb kanti
 */
public class AddBase extends javax.swing.JFrame 
{
    /**
     * Creates new form CreateBase
     */
    private GameScene gameScene;
    private Point2D point;
     private double lon;
    private double lat;
    
    public AddBase(GameScene gameScene, Point2D point1 )
    {
        
        this.gameScene = gameScene;
        this.point = point1;
        this.lon = OsmMercator.XToLon((int)(point.getX() + HTB_MAIN.translate_x), HTB_MAIN.currentZoom);
        this.lat = OsmMercator.YToLat((int)(-(point.getY() - HTB_MAIN.translate_y)), HTB_MAIN.currentZoom);
	
	//System.out.println("from addbase: x " + this.point.getX() + ", y " + this.point.getY());
	
	this.setLocation(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
	initComponents();
	
	//double x = this.point.getX()*HTB_MAIN.one_px_equivalent_naut;
	//double y = this.point.getY()*HTB_MAIN.one_px_equivalent_naut;
	
	String strX = String.valueOf(this.lon);
	String strY = String.valueOf(this.lat);
	//jTextFieldLocationX.setText(String.valueOf(this.point.getX()));
	jTextFieldLocationX.setText(strX);
	jTextFieldLocationY.setText(strY);
	
	
	
    }
    
    /*
    private String[] getAllAircraftTypes()
    {
        GenericTypeBuilder<AircraftType> genericTypeBuilder = new GenericTypeBuilder<>();
        List<AircraftType> listAircraftTypes = genericTypeBuilder.readAll("aircraftType.object");
        
        String[] strAircraftType = new String[listAircraftTypes.size()];
        
        for(int i=0;i<strAircraftType.length;i++)
        {
            strAircraftType[i] = listAircraftTypes.get(i).getTypename();
        }
        return strAircraftType;
    }
    */
    
    
    private String[] getAllBaseTypes()
    {
        
        List<BaseType> listBaseType = gameScene.baseTypeList;
        
        String[] strBaseType = new String[listBaseType.size()];
        
        for(int i=0;i<strBaseType.length;i++)
        {
            strBaseType[i] = listBaseType.get(i).getName();
        }
        return strBaseType;
    }
   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxBaseType = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldLocationX = new javax.swing.JTextField();
        jTextFieldLocationY = new javax.swing.JTextField();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Create Base");
        setAlwaysOnTop(true);

        jLabel1.setText("Base Type:");

        jComboBoxBaseType.setModel(new javax.swing.DefaultComboBoxModel(gameScene.getBaseTypeStringArray()));

        jLabel3.setText("Location(lon):");

        jLabel4.setText("Location(lat):");

        jButtonOk.setText("Ok");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldLocationX, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldLocationY))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jComboBoxBaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButtonOk)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancel)))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxBaseType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldLocationX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldLocationY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonCancel))
                .addContainerGap(117, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
	
	int index = jComboBoxBaseType.getSelectedIndex();
        
         double fLon = Double.parseDouble(jTextFieldLocationX.getText());
            double fLat = Double.parseDouble(jTextFieldLocationY.getText());
            
            int fX = OsmMercator.LonToX(fLon, HTB_MAIN.currentZoom);
            int fY = OsmMercator.LatToY(fLat, HTB_MAIN.currentZoom);
            
            double x = (fX - HTB_MAIN.translate_x);
	    double y = (-fY + HTB_MAIN.translate_y);
            
        
	Base base = new Base(gameScene.baseTypeList.get(index), new Location((x)* gameScene.HTBParent.one_nm_equivalent_pixel, (y) * gameScene.HTBParent.one_nm_equivalent_pixel, 0.0));
	
	gameScene.baseList.add(base);
        gameScene.HTBParent.repaint();
	//System.out.println("from AddBAse " + gameScene.baseList.size());
	this.setVisible(false);
	this.dispose();
	
	
        //GeneralUtil.UpdateGameScene(gameScene);
	
	RecordedGame recordedGame = gameScene.HTBParent.getRecordedGame();	    
	if(recordedGame!=null)
	{
	    if(recordedGame.isRecordingStarted())
	    {
		recordedGame.storeGameScene(gameScene);
	    }
	}
	
    }//GEN-LAST:event_jButtonOkActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddBase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddBase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddBase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddBase.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddBase(null,null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JComboBox jComboBoxBaseType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextFieldLocationX;
    private javax.swing.JTextField jTextFieldLocationY;
    // End of variables declaration//GEN-END:variables
}
